#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include "common.h"

/* How much space to leave between the stacks, at each end */
#define REDZONE	_MT_PAGE_SIZE(0)

_mt_stack_t *_mt_stack_new(uint32_t stack_size)
{
	_mt_stack_t *ts;
	uint32_t extra = 0;

	/* Make a new thread stack object. */
	if ((ts = (_mt_stack_t *)calloc(1, sizeof(_mt_stack_t))) == NULL)
		return NULL;
	
	ts->vaddr_size = stack_size + 2*REDZONE + extra;
	ts->vaddr = (char *)malloc(ts->vaddr_size);
	if (!ts->vaddr) {
		free(ts);
		return NULL;
	}
	
	ts->stk_size = stack_size;
	ts->stk_bottom = ts->vaddr + REDZONE;
	ts->stk_top = ts->stk_bottom + stack_size;
	return ts;
}


void _mt_stack_free(_mt_stack_t *ts)
{
	if (!ts)
		return;

	free(ts->vaddr);
	free(ts);
	return;
}


